package com.example.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Controller.SQLController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class getListHighlight {//@RequestMapping("/List")
    @PostMapping("/getListHightlight")//url: 'http://localhost:8080/List/getHighlight'
    public JSONObject GetListHighlight(@RequestBody JSONObject requestBody){
        SQLController sqlController=new SQLController();
        JSONObject json = new JSONObject();
        JSONArray arrayPlayer = new JSONArray();
        Connection connection=sqlController.ConnectToSql("jdbc:mysql://localhost:3306/jidazhidemai","root","gFL123456"+"");
        if(connection!=null){
            try {

                Statement statement=connection.createStatement();
                ResultSet resultSet=statement.executeQuery("select * from highlight ");

                JSONObject data = new JSONObject();
                while (resultSet.next()){
                    json.put("status","COMPLETE");
                    System.out.println(resultSet.getInt("postid"));
                    String postid= String.valueOf(resultSet.getInt("postid"));
                    data=queryDb(postid);
                    arrayPlayer.add(data);
                }

                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        json.put("list",arrayPlayer);
        return json;
    }
    public static JSONObject queryDb(String postid) throws SQLException {
        Connection con=null;
        JSONObject data  = new JSONObject();
        PreparedStatement preparedStatement = null;
        try {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/jidazhidemai";

            // 数据库的用户名与密码，需要根据自己的设置
            String user = "root";
            String password = "gFL123456";
            Class.forName(driver);
            //注册JDBC驱动程 序
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            //Statement stmt = con.createStatement();
//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String sql = " select * from post where postid=?;";
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, postid);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                String title = rs.getString("name");
                String location = rs.getString("location");
                int upvote = rs.getInt("like");
                int downvote = rs.getInt("dislike");
                Boolean fav = queryfav(rs.getString("posterid"), postid);
                String comments = rs.getString("comments");
                JSONArray arrayPlayer = new JSONArray();
                arrayPlayer = JSONArray.parseArray(comments);
                if(arrayPlayer==null){
                    arrayPlayer = new JSONArray();
                }
                System.out.println(fav);
                data.put("name", title);
                data.put("location", location);
                data.put("postid", rs.getString("postid"));
                data.put("like", upvote);
                data.put("dislike",downvote);
                data.put("comments", arrayPlayer.size());
                data.put("fav", fav);
                data.put("oldprice", rs.getDouble("oldprice"));
                data.put("newprice", rs.getDouble("newprice"));
                data.put("time", rs.getString("time"));
            }
            con.close();
            return data;

        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        data.put("status", "FAIL_NOMATCH");
        return data;
    }
    public static boolean queryfav (String userid, String postid){
        Connection con;
        try {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/jidazhidemai";

            // 数据库的用户名与密码，需要根据自己的设置
            String user = "root";
            String password = "gFL123456";
            Class.forName(driver);
            //注册JDBC驱动程 序
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            //Statement stmt = con.createStatement();
//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String sql = " select fav from user where userid=?;";
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, userid);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                String fav = rs.getString("fav");
                List<String> result = Arrays.asList(fav.split(","));
                if (result.contains(postid)) {
                    con.close();
                    return true;
                } else {
                    con.close();
                    return false;
                }
            }


        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }

        return false;
    }
}
